<?php
	class Privilege extends MY_controller{

		function __construct(){
			parent::__construct();
			
		}

		/**
		* 函数功能介绍：返回菜单列表
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-20 16:14
		* 最后修改人：
		* 最后修改时间：
		*/
		function index($pid = 'all', $sort = 0, $asc_des = 0, $search = 0, $limit = 0){
			if($this->input->post()){
				$limit = 0;
				$post_data = $this->input->post();
				$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
				$pid = $post_data['pid'];
			}else{
				$search = urldecode($search);
				$page_config['uri_segment'] = 7;
			}
			$offset = 10;
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$conditions = array('pid' => $pid, 'sort' => $sort, 'asc_des' => $asc_des, 'search' => $search, 'limit' => $limit);
			$result = $this->privileges->get_list_ofpage($limit, $offset, $conditions);
			$this->load->library('pagination');
			$page_config['base_url'] = '/privilege/index/' . $pid . '/' . $sort . '/' . $asc_des . '/' . $search;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $offset; 
			
			$this->pagination->initialize($page_config); 
			$pagination = $this->pagination->create_links();
			$privilege_list = $this->privileges->get_list();
			$this->smarty->assign("privilege_list", $privilege_list);
			$this->smarty->assign("privilege_data", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->assign("action", '权限菜单列表');
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->display("privilege/list.html");
		}

		/**
		* 函数功能介绍：添加菜单列表
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-20 10:24
		* 最后修改人：
		* 最后修改时间：
		*/
		function add(){
			$this->smarty->assign("action", '添加权限菜单');
			$privilege_list = $this->privileges->get_list();
			$this->smarty->assign("privilege_list", $privilege_list);
			$this->smarty->display("privilege/save.html");
		}

		/**
		* 函数功能介绍：保存菜单列表
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-20 11:44
		* 最后修改人：
		* 最后修改时间：
		*/
		function save(){
			if( ! $this->input->post()) {
				show_message('/privilege', "非法操作！");exit;
			}
			$data = $this->input->post();
			if($data['id'] > 0){
				if(!isset($data['status'])) $data['status'] = 0;
				$this->update_privilege($data);
			}else{
				$this->add_privilege($data);
			}
		}

		/**
		* 函数功能介绍：编辑权限菜单
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-20 18:11
		* 最后修改人：
		* 最后修改时间：
		* @param id int
		*/
		function edit($id){
			$privilege_list = $this->privileges->get_list();
			$this->smarty->assign("privilege_list", $privilege_list);
			$privilege_info = $this->privileges->get_privilege_info($id);
			$this->smarty->assign("action", '编辑权限菜单');
			$this->smarty->assign("privilege_info", $privilege_info[0]);
			$this->smarty->display("privilege/save.html");
		}

		/**
		* 函数功能介绍：查看权限菜单
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-21 09:44
		* 最后修改人：
		* 最后修改时间：
		* @param id int
		*/
		function view($id){
			$privilege_info = $this->privileges->get_privilege_info($id);
			if($privilege_info[0]->pid > 0){
				$p_info = $this->privileges->get_privilege_info($privilege_info[0]->pid);
				$this->smarty->assign("p_name", $p_info[0]->menu_title);
			}
			$this->smarty->assign("action", '查看权限菜单');
			$this->smarty->assign("privilege_info", $privilege_info[0]);
			$this->smarty->display("privilege/view.html");
		}

		/**
		* 函数功能介绍：删除权限菜单
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-21 10:04
		* 最后修改人：
		* 最后修改时间：
		* @param ids int
		*/
		function delete($id = 0){
			if($_POST){
				$ids = $this->input->post('ids');
				if(count($ids) > 0){
					$this->privileges->batch_delete($ids);
					show_message($_SERVER['HTTP_REFERER'], "权限信息删除成功");
				}else{
					show_message($_SERVER['HTTP_REFERER'], "您没有选中任何数据");exit;
				}
			}elseif($id > 0){
				$result = $this->privileges->delete($id);
				if($this->db->affected_rows() > 0){
					show_message($_SERVER['HTTP_REFERER'], "权限信息删除成功");
				}else{
					show_message($_SERVER['HTTP_REFERER'], "权限信息删除失败，请重试！");
				}
			}
		}

		/**
		* 添加角色数据
		* Create: 	Zhou Hailong
		* Cretaed: 2014-08-22 16:33
		* 最后修改人：
		* Last update：
		*/
		function add_privilege($data){
			if(trim($data['menu_url']) == ''){
				show_message($_SERVER['HTTP_REFERER'], "菜单URL不能为空！");exit;
			}
			$privilege_info = $this->privileges->get_field_by_where("id", "menu_url='{$data['menu_url']}' OR menu_title='{$data['menu_title']}'");
			if(isset($privilege_info['id']) && $privilege_info['id'] > 0){
				show_message('-1', "您添加的权限菜单已存在");exit;
			}
			$data['add_time'] = time();
			$result = $this->privileges->insert($data);
			if($result == TRUE){
				show_message($_SERVER['HTTP_REFERER'], "权限菜单添加成功！");exit;
			}else{
				show_message($_SERVER['HTTP_REFERER'], "权限菜单添加失败，请重试！");exit;
			}
		}


		/**
		* 修改角色数据
		* Create: 	Zhou Hailong
		* Cretaed: 2014-08-22 15:09
		* 最后修改人：
		* Last update：
		*/
		function update_privilege($data){
			$privileges = $this->privileges->get_field_by_where("id", "(menu_url = '{$data['menu_url']}' OR menu_title='{$data['menu_title']}') AND id != '{$data['id']}'");
			if(isset($privileges['id']) && $privileges['id'] > 0){
				show_message('/privilege', "该权限菜单已存在");exit;
			}
			$this->db->where('id', $data['id']);
 			$this->db->update('sys_privileges', $data);
 			show_message($_SERVER['HTTP_REFERER'], "权限菜单修改成功");
		}

		function update_sorts(){
			$sort = $this->input->post('sort');
			if(is_array($sort) && count($sort) > 0){
				foreach($sort as $key=>$val) {
					$this->privileges->update_privilege_sort($key, array('sort'=>$val));
				}
			}
			show_message($_SERVER['HTTP_REFERER'], "排序修改成功");
		}

		function status(){
			$return = array();
			if( !$this->input->get() || !$this->input->get('id')) {
				$return = array('code'=>9,'msg'=>'非法操作！');
			}else {
				$this->db->where('id', $this->input->get('id'));
 			 	$result = $this->db->update('sys_privileges', array('status'=>$this->input->get('status')));
				if($result) {
					$return = array('code'=>0,'msg'=>'成功修改');
				}else {
					$return = array('code'=>1,'msg'=>'修改失败');
				}
			}
			echo json_encode($return);exit;
		}
	}